package com.tricode.ile.training.task.mite;

import java.io.*;
import java.util.*;

public class LastNameSort {

    public static void main(String[] args) {
        try {
            Properties properties = getPropertiesFromFile("test.properties");
            List<Person> persons1 = generatePersonsFromFileFormatOne(getPropertyFromProperties(properties, "input.file1"));
            List<Person> persons2 = generatePersonsFromFileFormatTwo(getPropertyFromProperties(properties, "input.file2"));
            List<Person> persons3 = joinPersons(persons1, persons2);
//            sortByLastName(persons3);
            sortBy(persons3, "age");
            writePersonsToFile(persons3, getPropertyFromProperties(properties, "output.file1"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<Person> joinPersons(List<Person> persons1, List<Person> persons2) {
        for (Person p : persons2) {
            persons1.add(p);
        }
        return persons1;
    }

    private static String getPropertyFromProperties(Properties properties, String property) {
        return properties.getProperty(property);
    }

    private static Properties getPropertiesFromFile(String propertiesFile) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(propertiesFile));
        return properties;
    }

    private static void writePersonsToFile(List<Person> persons, String outputDir) throws IOException {
        File file = new File(outputDir);
        PrintWriter printWriter = new PrintWriter(file);
        for (Person p : persons) {
            printWriter.write(p.toString());
        }
        printWriter.flush();
        printWriter.close();
    }

    private static List<Person> generatePersonsFromFileFormatOne(String filePath) throws IOException {
        return generatePersonsFromFormater(filePath, 1);
    }

    private static List<Person> generatePersonsFromFileFormatTwo(String filePath) throws IOException {
        return generatePersonsFromFormater(filePath, 2);
    }

    private static List<Person> generatePersonsFromFormater(String filePath, int formater) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        List<Person> persons = new ArrayList<Person>();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            String[] personArray = line.split("\\s+");
            switch (formater) {
                case 1:
                    Person p1 = createAPersonFormatOne(personArray);
                    persons.add(p1);
                    continue;
                case 2:
                    Person p2 = createAPersonFormatTwo(personArray);
                    persons.add(p2);
                    continue;
            }
        }
        return persons;
    }

    private static Person createAPersonFormatTwo(String[] personArray) {
        String lastName = personArray[0];
        String name = personArray[1];
        int age = Integer.parseInt(personArray[2]);
        String gender = personArray[3];
        Date date = generateDateFromString(personArray[4], "-+");
        return new Person(name, lastName, age, gender, date);
    }

    private static void sortByLastName(List<Person> persons) {
        Collections.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person person1, Person person2) {
                return person1.getLastName().compareTo(person2.getLastName());
            }
        });
    }

    private static void sortBy(List<Person> persons, String sortValue) {
        if (sortValue.equalsIgnoreCase("last name")) {
            Collections.sort(persons, new Comparator<Person>() {
                @Override
                public int compare(Person person1, Person person2) {
                    return person1.getLastName().compareTo(person2.getLastName());
                }
            });
        }
        if (sortValue.equalsIgnoreCase("first name")) {
            Collections.sort(persons, new Comparator<Person>() {
                @Override
                public int compare(Person person1, Person person2) {
                    return person1.getName().compareTo(person2.getName());
                }
            });
        }

        if (sortValue.equalsIgnoreCase("age")) {
            Collections.sort(persons, new Comparator<Person>() {
                @Override
                public int compare(Person person1, Person person2) {
                    if (person1.getAge() == person2.getAge())
                        return 0;
                    else if (person1.getAge() > person2.getAge())
                        return 1;
                    else
                        return -1;
                }
            });
        }
    }

    private static Person createAPersonFormatOne(String[] personArray) {
        String name = personArray[0];
        String lastName = personArray[1];
        int age = Integer.parseInt(personArray[2]);
        Date date = generateDateFromString(personArray[3], "/+");
        return new Person(name, lastName, age, date);
    }

    private static Date generateDateFromString(String dateString, String regex) {
        String[] dateArray = dateString.split(regex);
        int day = Integer.parseInt(dateArray[0]);
        int month = Integer.parseInt(dateArray[1]);
        int year = Integer.parseInt(dateArray[2]);
        return new Date(day, month, year);
    }
}

class Person {

    private String name;
    private String lastName;
    private int age;
    private String gender;
    private Date date;

    Person(String name, String lastName, int age, String gender, Date date) {
        this(name, lastName, age, date);
        this.gender = gender;
    }

    Person(String name, String lastName, int age, Date date) {
        this.lastName = lastName;
        this.name = name;
        this.age = age;
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getAge() {
        return age;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        if (this.gender == null) {
            this.gender = "";
        }
        String newLine = System.getProperty("line.separator");
        String print = lastName + " " + name + " " + age + " " + gender + " " + date + newLine;
        return print;
    }
}

class Date {

    private int day;
    private int month;
    private int year;

    Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return day + "-" + month + "-" + year;
    }
}

